// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  const openId = wxContext.OPENID;
  const limit = event.limit || 20;
  const skip = event.skip || 0;

  const out = await cloud.database().collection('favorites').aggregate().match({
    _openid: openId
  }).limit(limit).skip(skip).lookup({
    from: 'books',
    localField: 'book',
    foreignField: '_id',
    as: 'book'
  }).end();

  return {
    result: out
  };
}
